package com.wwg.geoquiz

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.annotation.ContentView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

private const val EXTRA_Answer = "answer"
private const val EXTRA_Result = "result"
private const val EXTRA_Is_Cheat = "cheat"
private const val TAG = "WWG IS HANDSOM"
class CheatActivity : Activity() {
    private var answerIsTrue = false
    private var quizIndex : Int = 0
    private lateinit var textView: TextView
    private lateinit var showAnswerbutton: Button
    private var cheatTag: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG,"onCreate")
        setContentView(R.layout.activity_cheat)

        //答案
        answerIsTrue = intent.getBooleanExtra(EXTRA_Answer, false)
        //作弊状态
        cheatTag = savedInstanceState?.getBoolean(EXTRA_Is_Cheat,false) == true

        showAnswerbutton = findViewById<Button>(R.id.show_answer_button)
        textView = findViewById<TextView>(R.id.answer_text_view)

        showAnswerbutton.setOnClickListener { view ->
            val text = when {
                //将Boolean类型巧妙的转化成了string字符串
                answerIsTrue -> R.string.button_true
                else -> R.string.button_false
            }
            cheatTag = true
            textView.setText(text)
//            showAnswerCheating()
        }
        showAnswerCheating()
    }

    private fun showAnswerCheating() {
        val data = Intent().apply {
            putExtra(EXTRA_Result, cheatTag)
        }
        setResult(101, data)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean(EXTRA_Is_Cheat,cheatTag)
    }

    companion object {
        fun newIntent(packageContext: Context, answerIsTrue: Boolean): Intent {
            return Intent(packageContext, CheatActivity::class.java).apply {
                putExtra(EXTRA_Answer, answerIsTrue)
            }
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onstart()")

    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPaues()")

    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume()")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy()")
        Log.d(TAG,""+cheatTag)

    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")

    }
}